var app = getApp();

/**
 * 拼团列表，红包拼团返回数量是1
 */
function getGroupList(userId, groupBuyType, success,fail) {
  let param = new Object();
  param['userId'] = userId;
  param['groupBuyType'] = groupBuyType;
  app.func.Get(true, app.api.groupbuy_new_list, param, function(res) {
    typeof success == 'function' && success(res);
  }, function (error) {
    typeof fail == 'function' && fail(error);
  });
}

/**
 * 获取拼团列表详情
 * @param groupBuyType （0：闪团 1：拼团 2红包拼团）
 */
function getGroupDetailNew(userId, groupBuyId, groupBuyUserId, groupBuyType, success) {
  let param = new Object();
  param['userId'] = userId;
  param['groupBuyId'] = groupBuyId;
  param['groupBuyUserId'] = groupBuyUserId;
  param['groupBuyType'] = groupBuyType;
  app.func.Post(false, app.api.groupbuy_detail_new, param, function(res) {
    typeof success == 'function' && success(res);
  });
}

/**
 * 团长开团
 */
function confirmGroupRedPacket(groupBuyId, addressId, success, fail) {
  let param = new Object();
  param['userId'] = app.globalData.userId;
  param['groupBuyId'] = groupBuyId;
  param['addressId'] = addressId;
  app.func.Post(false, app.api.groupbuy_confirm_confirm_redpacket, param,
    function(res) {
      typeof success == 'function' && success(res);
    },
    function(res) {
      typeof fail == 'function' && fail(res);
    });
}

/**
 * 参与拼团
 */
function helpGrouperRedPacket(groupBuyId, grouperId, groupBuyUserId, success, fail) {
  let param = new Object();
  param['userId'] = app.globalData.userId;
  param['groupBuyId'] = groupBuyId;
  param['grouperId'] = grouperId;
  param['groupBuyUserId'] = groupBuyUserId;
  app.func.Post(false, app.api.groupbuy_help_grouper_redpacket, param,
    function(res) {
      typeof success == 'function' && success(res);
    },
    function(res) {
      typeof fail == 'functionh' ** fail(res);
    });
}

/**
 * 查询用户有无红包详情
 */
function getGroupbuypacketdetail(groupBuyUserId, success) {
  let param = new Object();
  param['groupBuyUserId'] = groupBuyUserId;
  app.func.Post(false, app.api.groupbuy_packetdetail, param, function(res) {
    typeof success == 'function' && success(res);
  });
}

/**
 * 查询用户在团中的状态
 */
function checkUserGroupBuyStatus(userId, groupBuyId, success) {
  let param = new Object();
  param['userId'] = userId;
  param['groupBuyId'] = groupBuyId;
  app.func.Post(false, app.api.groupbuy_user_status, param, function(res) {
    typeof success == 'function' && success(res);
  });
}

module.exports = {
  getGroupList: getGroupList,
  getGroupDetailNew: getGroupDetailNew,
  confirmGroupRedPacket: confirmGroupRedPacket,
  helpGrouperRedPacket: helpGrouperRedPacket,
  getGroupbuypacketdetail: getGroupbuypacketdetail,
  checkUserGroupBuyStatus: checkUserGroupBuyStatus
}